package practise.leetcode.q3;

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        int maxLength = 0;
        for (int i = 0; i < chars.length; i++) {
            int len = getLength(i, chars);
            if (len > maxLength) {
                maxLength = len;
            }

            if (maxLength == chars.length) {
                break;
            }
        }
        return maxLength;
    }

    private int getLength(int startIdx, char[] chars) {
        int sum = 0;
        Map<Character, Integer> ch = new HashMap<>(chars.length);;
        for (int i = startIdx; i < chars.length; i++) {
            char c = chars[i];
            if (ch.containsKey(c)) {
                break;
            }

            ch.put(c, 1);
            sum++;
        }
        return sum;
    }
}
